//
//  CCAdViewController.m
//  BuDeJie
//
//  Created by demoncraz on 2017/2/10.
//  Copyright © 2017年 demoncraz. All rights reserved.
//

#import "CCAdViewController.h"
#import "CCTabBarController.h"
#import <AFNetworking.h>
#import <MJExtension/MJExtension.h>
#import <UIImageView+WebCache.h>

#import "CCAdItem.h"

#define code2 @"phcqnauGuHYkFMRquANhmgN_IauBThfqmgKsUARhIWdGULPxnz3vndtkQW08nau_I1Y1P1Rhmhwz5Hb8nBuL5HDknWRhTA_qmvqVQhGGUhI_py4MQhF1TvChmgKY5H6hmyPW5RFRHzuET1dGULnhuAN85HchUy7s5HDhIywGujY3P1n3mWb1PvDLnvF-Pyf4mHR4nyRvmWPBmhwBPjcLPyfsPHT3uWm4FMPLpHYkFh7sTA-b5yRzPj6sPvRdFhPdTWYsFMKzuykEmyfqnauGuAu95Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiu9mLfqHbD_H70hTv6qnHn1PauVmynqnjclnj0lnj0lnj0lnj0lnj0hThYqniuVujYkFhkC5HRvnB3dFh7spyfqnW0srj64nBu9TjYsFMub5HDhTZFEujdzTLK_mgPCFMP85Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiuBnHfdnjD4rjnvPWYkFh7sTZu-TWY1QW68nBuWUHYdnHchIAYqPHDzFhqsmyPGIZbqniuYThuYTjd1uAVxnz3vnzu9IjYzFh6qP1RsFMws5y-fpAq8uHT_nBuYmycqnau1IjYkPjRsnHb3n1mvnHDkQWD4niuVmybqniu1uy3qwD-HQDFKHakHHNn_HR7fQ7uDQ7PcHzkHiR3_RYqNQD7jfzkPiRn_wdKHQDP5HikPfRb_fNc_NbwPQDdRHzkDiNchTvwW5HnvPj0zQWndnHRvnBsdPWb4ri3kPW0kPHmhmLnqPH6LP1ndm1-WPyDvnHKBrAw9nju9PHIhmH9WmH6zrjRhTv7_5iu85HDhTvd15HDhTLTqP1RsFh4ETjYYPW0sPzuVuyYqn1mYnjc8nWbvrjTdQjRvrHb4QWDvnjDdPBuk5yRzPj6sPvRdgvPsTBu_my4bTvP9TARqnam"

@interface CCAdViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *launchImageView;
@property (weak, nonatomic) IBOutlet UIView *containerView;
@property (weak, nonatomic) IBOutlet UIButton *skipButton;

@property (nonatomic, weak) UIImageView *adImageView;
@property (nonatomic, strong) CCAdItem *item;
@property (nonatomic, weak) NSTimer *timer;

@end

@implementation CCAdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupLaunchImage];
    
    //请求服务器的数据
    [self loadAdData];
    
    //设置定时器
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
}

- (void)timeChange {
    static NSInteger second = 3;
    
    second --;
    
    [self.skipButton setTitle:[NSString stringWithFormat:@"跳转（%zd）", second] forState:UIControlStateNormal];
    
    
    if (second == 0) {
        [self skipButtonClick];
    }
}



#pragma mark - lazy loading

- (UIImageView *)adImageView {
    if (_adImageView == nil) {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
        [imageView addGestureRecognizer:tap];
        
        [self.containerView addSubview:imageView];
        _adImageView = imageView;
    }
    return _adImageView;
}

- (void)tap {
    UIApplication *application = [UIApplication sharedApplication];
    if ([application canOpenURL:[NSURL URLWithString:self.item.ori_curl]]) {
        [application openURL:[NSURL URLWithString:self.item.ori_curl]];
    }
}

#pragma mark - 请求广告数据df

- (void)loadAdData {
    
    //1.创建请求会话管理者
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    
    //请求拼接参数
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    
    parameters[@"code2"] = code2;
    
    //2.发送请求
    [mgr GET:@"http://mobads.baidu.com/cpro/ui/mads.php" parameters:parameters progress:nil
     success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *responseObject) {
         
         [responseObject writeToFile:@"/Users/demoncraz/Desktop/LuyiTechRelated/Ad.plist" atomically:YES];
         NSDictionary *dataDict = [responseObject[@"ad"] firstObject];
         
         //转化成模型
         CCAdItem *item = [CCAdItem mj_objectWithKeyValues:dataDict];
         _item = item;
         //创建imageView展示图片
         CGFloat adImageH = CCScreenW / item.w * item.h;
         self.adImageView.frame = CGRectMake(0, 0, CCScreenW, adImageH);
         [self.adImageView sd_setImageWithURL:[NSURL URLWithString:item.w_picurl]];
         
         
     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
         NSLog(@"请求失败");
         NSLog(@"%@", error);
     }];
    
    
}


- (void)setupLaunchImage {
    
    //屏幕适配
    if (iPhone4) {
        self.launchImageView.image = [UIImage imageNamed:@"LaunchImage"];
    } else if (iPhone5) {
        self.launchImageView.image = [UIImage imageNamed:@"LaunchImage-700-568h@2x"];
    } else if (iPhone6_7) {
        self.launchImageView.image = [UIImage imageNamed:@"LaunchImage-800-667h@2x"];
    } else if (iPhone6p_7p) {
        self.launchImageView.image = [UIImage imageNamed:@"LaunchImage-800-Portrait-736h@3x"];
    }
    
    
    
    
}
- (IBAction)skipButtonClick {
    
    //切换根控制器
    [UIApplication sharedApplication].keyWindow.rootViewController = [[CCTabBarController alloc] init];
    //移除定时器
    [self.timer invalidate];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
